package com.example.controller;


import com.example.pojo.Result;
import com.example.pojo.PageBean;
import com.example.service.impl.UserServiceImpl;
import com.example.vd.vo.LoginVo;
import com.example.vd.vo.StatusVo;
import com.example.vd.vo.UserVo;
import jakarta.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author fzy'
 * @since 2024-08-08
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/login")
    public Result getByUser(@ModelAttribute LoginVo loginVo) {
        log.info("/login 参数: {}", loginVo);
        return userService.tryLogin(loginVo);
    }

    @GetMapping("/list")
    public Result getUserList(@ModelAttribute PageBean pageBean,@Nullable @RequestParam(defaultValue = "") String nickname
            ,@Nullable @RequestParam(defaultValue = "") String idCard) {
        log.info("/list 参数: {},{},{}", pageBean,idCard,nickname);
        return userService.getUserList(pageBean,idCard,nickname);
    }

    @DeleteMapping("/delete")
    public Result deletedById(Long id) {
        log.info("/deleted 参数: {}", id);
        return userService.deletedById(id);
    }

    @PutMapping("/update")
    public Result updateByUser(@RequestBody UserVo user) {
        log.info("/update 参数: {}", user);
        return userService.updateByUser(user);
    }

    @PutMapping("/updateStatus")
    public Result changeStatus(@RequestBody StatusVo statusVo) {
        log.info("/updateStatus 参数: {}", statusVo);
        return userService.changeStatus(statusVo.getId(), statusVo.getStatus());
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody UserVo user) {
        log.info("/insert 参数: {}", user);
        return userService.insertByUser(user);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserVo user) {
        log.info("/register 参数: {}", user);
        return userService.registerByUser(user);
    }
}
